var bookSearchBtn = document.getElementById("book-search-btn"),
    bookSearchContent = document.getElementById("book-search-content"),
    bookSearchWaiting = document.getElementById("book-search-waiting"),
    bookSearchResultBox = document.getElementById("book-search-result-box"),
    bookFirstContentBox = document.getElementById("book-first-content-box"),
    bookSecondContentBox = document.getElementById("book-second-content-box"),
    bookThirdContentBox = document.getElementById("book-third-content-box"),
    bookForthContentBox = document.getElementById("book-forth-content-box")
    bookDetailContentBox = document.getElementById("book-detail-content-box");

// 返回的图书图片大小比例不一需用js进行动态样式调整
// function bookImgAdjust() {
//     var imgs = document.getElementsByClassName("book-search-result-img");
//     // console.log(bookSearchResultBox);
//     var imgWidth = imgs[0].offsetWidth;
//     // console.log(boxWidth);
//     // var imgWidth = imgs[0].style.width;
//     console.log(imgWidth);
//     for (var i = 0; i < imgs.length; i++) {
//         imgs[i].setAttribute("height",imgWidth / 3 * 4);
//     }
// }
// var bookTopxxContentBox = document.getElementById("book-topxx-content-box");
// top20模块
// function bookTopxxx()
// {
//     var xmlhttp;
//     if (window.XMLHttpRequest)
//     {
//         //  IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
//         xmlhttp=new XMLHttpRequest();
//     }
//     else
//     {
//         // IE6, IE5 浏览器执行代码
//         xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
//     }
//     xmlhttp.onreadystatechange=function()
//     {
//         if (xmlhttp.readyState==4 && xmlhttp.status==200)
//         {
//             // console.log(xmlhttp.responseText);
//             // console.log("4");
//             // 搜索完毕隐藏动画div
//             // searchWaiting.style.display = "none";
//             var result = JSON.parse(xmlhttp.responseText);
//             // showResult(searchResult);
//             // console.log(searchResult);
//             var tempData = result.showapi_res_body.pagebean;
//             console.log(tempData);
//             var html = template("topTemp", tempData);
//             // console.log(html);
//             bookTopxxContentBox.innerHTML = html;
//         }
//     }
//     // https://route.showapi.com/213-1?keyword=海阔天空&page=1&showapi_appid=31921&showapi_sign=7b3f1d2bac974bdf83fdd5e052d988fd
//     xmlhttp.open("GET","https://route.showapi.com/213-4?topid=23&showapi_appid=31921&showapi_sign=7b3f1d2bac974bdf83fdd5e052d988fd",true);
//     xmlhttp.send();
// }


// 搜书模块
// JSONP
var bookDataSearch;
function bookSearchCallback(result) {
    //    alert(result);
    console.log(result);
    bookSearchWaiting.style.display = "none";

    var html = template("bookSearchTemp", result);
    // console.log(html);
    bookSearchResultBox.innerHTML = html;
    bookDataSearch = result;
    // console.log("123");


}

function searchBook()
{
    var JSONP=document.createElement("script");
    JSONP.type="text/javascript";
    JSONP.src="https://api.douban.com/v2/book/search?q="+bookSearchContent.value+"&callback=bookSearchCallback";
    document.getElementsByTagName("head")[0].appendChild(JSONP);
}

// 分类图书
// 进入页面后就开始发送请求获取各类图书

var bookDataFirst;//由于data-*属性的局限  所以我们将四组数据保存在这里  以方便点击详情使用 从而不用再次查询
var bookDataSecond;
var bookDataThird;
var bookDataForth;
function bookFirstCallback(result) {
    var html = template("bookClassTemp", result);
    bookFirstContentBox.innerHTML = html;
    bookDataFirst = result;
}
function bookSecondCallback(result) {
    console.log(result);
    var html = template("bookClassTemp", result);
    bookSecondContentBox.innerHTML = html;
    bookDataSecond = result;
}
function bookThirdCallback(result) {
    var html = template("bookClassTemp", result);
    bookThirdContentBox.innerHTML = html;
    bookDataThird = result;
}
function bookForthCallback(result) {
    var html = template("bookClassTemp", result);
    bookForthContentBox.innerHTML = html;
    bookDataForth = result;
    // 所有块布局完成后调整方块
    // imgAdjust();
}
function bookCalss(value,callback)
{
    var JSONP=document.createElement("script");
    JSONP.type="text/javascript";
    JSONP.src="https://api.douban.com/v2/book/search?tag="+value+"&callback="+callback;
    document.getElementsByTagName("head")[0].appendChild(JSONP);
}

bookCalss("文学","bookFirstCallback");
// setTimeout(function () {
    bookCalss("科技","bookSecondCallback");
// },3000);
// setTimeout(function () {
    bookCalss("流行","bookThirdCallback");
// },6000);
// setTimeout(function () {
    bookCalss("生活","bookForthCallback");
// },9000);
//
// setTimeout(function () {
//     bookImgAdjust();
// },10000);


bookSearchBtn.onclick = function(event){
    console.log("123");
    console.log(bookSearchContent.value);
    if(bookSearchContent.value){
        // 搜索等待动画
        bookSearchWaiting.style.display = "block";
        searchBook();
    }
};


//book 详情渲染
function bookResultDetail(button) {
    console.log(button.parentNode.parentNode);
    switch (button.parentNode.parentNode.getAttribute("id")) {
        case "book-first-content-box":
        console.log();
            bookResultRender(bookDataFirst,button.parentNode.dataset.index);
            break;
        case "book-second-content-box":
            bookResultRender(bookDataSecond,button.parentNode.dataset.index);
            break;
        case "book-third-content-box":
            bookResultRender(bookDataThird,button.parentNode.dataset.index);
            break;
        case "book-forth-content-box":
            bookResultRender(bookDataForth,button.parentNode.dataset.index);
            break;
        default:
            bookResultRender(bookDataSearch,button.parentNode.dataset.index);
            break;
    }
    // bookAudioSource.setAttribute("title",button.parentNode.dataset.title);
    // bookAudio.setAttribute("src",button.parentNode.dataset.src);
    // bookAudioSource.setAttribute("data-singer",button.parentNode.dataset.singer);
    // if (button.parentNode.dataset.img) {
    //     bookAudioSource.setAttribute("data-img",button.parentNode.dataset.img);
    // }else {
    //     bookAudioSource.setAttribute("data-img","./img/no_img_song.jpg");
    // }
    //
    // console.log(button.parentNode);
    // // console.log(source);
    // setTimeout(function () {
    //     bookControlsPlay.click();
    // }, 500);
}
function bookResultRender(result, index) {
    console.log("boook================================");
    console.log(result);
    // console.log(result.books[index].summary);
    // result.books[index].summary = result.books[index].summary.replace(/[\n]/g,"<br>");
    // console.log(result.books[index].summary);
    var html = template("bookDetailTemp", result.books[index]);
    bookDetailContentBox.innerHTML = html;
}


function bookResultCardIn(div) {
    div.getElementsByTagName("button")[0].style.display="block";
}
function bookResultCardOut(div) {
    div.getElementsByTagName("button")[0].style.display="none";
}

// bookTopxxx();
